package com.atguigu.tingshu.user.client;

import com.atguigu.tingshu.common.cache.GuiguCache;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.user.service.UserPaidAlbumService;
import com.atguigu.tingshu.user.service.UserPaidTrackService;
import com.atguigu.tingshu.user.service.VipServiceConfigService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.method.P;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/***
 * 用户feign调用Controller
 */
@RestController
@RequestMapping("/client/user/userInfo")
public class UserClientController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserPaidTrackService userPaidTrackService;
    @Autowired
    private UserPaidAlbumService userPaidAlbumService;
    @Autowired
    private VipServiceConfigService vipServiceConfigService;

    /**
     * 根据id查询用户信息
     *
     * @param userId
     * @return
     */
    @GuiguCache(prefix = "getUserInfo:")
    @GetMapping("/getUserInfo/{userId}")
    public UserInfo getUserInfo(
            @PathVariable("userId") Long userId) {
        return userInfoService.getById(userId);
    }


    /**
     * 查询用户已购入的指定专辑的声音列表
     *
     * @param albumId
     * @param userId
     * @return
     */
    @GetMapping("/getUserPaidTrack/{albumId}/{userId}")
    public Map<String, Object> getUserPaidTrack(@PathVariable("albumId") Long albumId,
                                                @PathVariable("userId") Long userId) {
        return userPaidTrackService.getUserPaidTrack(albumId, userId);
    }


    /**
     * 查询用户购买的指定id的专辑
     * @param albumId
     * @param userId
     * @return
     */
    @GetMapping("/getUserPaidAlbum/{albumId}/{userId}")
    public Boolean getUserPaidAlbum(@PathVariable("albumId") Long albumId,
                                    @PathVariable("userId") Long userId) {
        return userPaidAlbumService.getOne(
                new LambdaQueryWrapper<UserPaidAlbum>()
                        .eq(UserPaidAlbum::getAlbumId,albumId)
                        .eq(UserPaidAlbum::getUserId,userId))!=null;
    }

    /**
     * 根据itemId查询VIP类型
     * @param itemId
     * @return
     */
    @GetMapping("/getVipServiceConfig/{itemId}")
    public VipServiceConfig getVipServiceConfig(@PathVariable("itemId")Long itemId){
        return vipServiceConfigService.getById(itemId);
    }

}
